package bugzillapreprocessor.parser;

import java.util.ArrayList;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import bugzillapreprocessor.model.Issue;
import bugzillapreprocessor.model.IssueBuilder;

/**
 * The XMLParseHandler class implements the call backs for the SAX parser
 * 
 * @author Mohamed Khayata
 */
public class XMLParseHandler extends DefaultHandler {

	private ArrayList<Issue> IssueList;
	private IssueBuilder anIssueBuilder;
	private Issue anIssue;
	private boolean dependsOnStart = false;
	private boolean blocksStart = false;
	private static String dependsOnList = "";
	private String info;
	private final String ISSUE = "issue", ISSUE_ID = "issue_id",
			ISSUE_TYPE = "issue_type", RESOLUTION = "resolution",
			ISSUE_STATUS = "issue_status", DELTA_TS = "delta_ts",
			CREATION_TS = "creation_ts", PRIORITY = "priority",
			REPORTER = "reporter", ASSIGNED_TO = "assigned_to",
			DEPENDENCE_ON = "dependson";

	/**
	 * Constructor
	 */
	public XMLParseHandler() {
		anIssueBuilder = IssueBuilder.getInstance();
		IssueList = new ArrayList<Issue>();
	}

	/**
	 * Get the list of issue objects
	 * 
	 * @return An array list of issue objects
	 */
	public ArrayList<Issue> getIssueList() {
		return IssueList;
	}

	// public static Issue getIssueByIndex(int index){
	// return IssueList.get(index);
	// }

	/**
	 * Extract the issue value from the XML tags and store them as properties in
	 * an issue object
	 * 
	 * @param elementName
	 */
	private void ExtractIssueInfo(String elementName) {
		if (elementName.equalsIgnoreCase(ISSUE_ID) && !dependsOnStart
				&& !blocksStart) {
			anIssueBuilder.withIssueId(info);
		}
		if (elementName.equalsIgnoreCase(ISSUE_TYPE)) {
			anIssueBuilder.withIssueType(info);
		}
		if (elementName.equalsIgnoreCase(RESOLUTION)) {
			anIssueBuilder.withResolution(info);
		}
		if (elementName.equalsIgnoreCase(ISSUE_STATUS)) {
			anIssueBuilder.withIssueStatus(info);
		}
		if (elementName.equalsIgnoreCase(DELTA_TS)) {
			anIssueBuilder.withDeltaTs(info);
		}
		if (elementName.equalsIgnoreCase(CREATION_TS)) {
			anIssueBuilder.withCreationTs(info);
		}
		if (elementName.equalsIgnoreCase(PRIORITY)) {
			anIssueBuilder.withPriority(info);
		}
		if (elementName.equalsIgnoreCase(REPORTER)) {
			anIssueBuilder.withReporter(info);
		}
		if (elementName.equalsIgnoreCase(ASSIGNED_TO)) {
			anIssueBuilder.withAssignedTo(info);
		}
		if (elementName.equalsIgnoreCase(ISSUE_ID) && dependsOnStart
				&& !blocksStart) {
			dependsOnList = dependsOnList + "|" + info;
			dependsOnStart = false;
		}
	}

	@Override
	public void startElement(String uri, String localName, String elementName,
			org.xml.sax.Attributes attributes) throws SAXException {

		if (elementName.equalsIgnoreCase(ISSUE)) {
			anIssueBuilder.startNewIssue();
		}

		if (elementName.equalsIgnoreCase(DEPENDENCE_ON)) {
			dependsOnStart = true;
		}

		if (elementName.equalsIgnoreCase("blocks")) {
			blocksStart = true;
		}

	}

	@Override
	public void endElement(String uri, String localName, String elementName)
			throws SAXException {

		if (elementName.equalsIgnoreCase(ISSUE)) {
			anIssueBuilder.withDependsOnIssueId(dependsOnList);
			dependsOnList = "";
			dependsOnStart = false;
			blocksStart = false;
			anIssue = anIssueBuilder.build();
			IssueList.add(anIssue);
		}

		if (!elementName.equalsIgnoreCase(ISSUE)) {
			ExtractIssueInfo(elementName);
		}
	}

	@Override
	public void characters(char ch[], int start, int length)
			throws SAXException {
		info = new String(ch, start, length);
	}
}
